﻿#region 命名空间

using System;
using System.Text.RegularExpressions;
using System.Web;
using WinnerCMS.Common;

#endregion

namespace WinnerCMS.PlugIn.OpenAPI.Passport
{
    public class PassportHelper
    {
        /// <summary>
        /// 本站自己用的。
        /// </summary>
        /// <returns></returns>
        public static IMember Instance()
        {
            return Instance("WinnerCMS.BLL", "WinnerCMS.BLL.Public_Member");
        }

        /// <summary>
        /// 給第三方整合用的。
        /// </summary>
        /// <param name="dllName"></param>
        /// <param name="fullCategoryName"></param>
        /// <returns></returns>
        public static IMember Instance(string dllName, string fullCategoryName)
        {
            if (dllName == null || fullCategoryName == null)
            {
                return Instance();
            }
            IMember iMember = null;

            if (string.IsNullOrEmpty(dllName))
            {
                throw new ArgumentNullException("未能调用插件");
            }

            iMember = CreateInstance<IMember>(dllName, fullCategoryName);
            if (iMember == null)
            {
                throw new ArgumentNullException("未能调用插件");
            }

            return iMember;
        }

        /// <summary>
        /// 创建类实例的方法，程序集名即为类命名空间
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="nameSpace"></param>   
        /// <param name="fullclassName">完全类名</param>
        /// <returns>类的实例</returns>
        private static T CreateInstance<T>(string nameSpace, string fullclassName)
        {
            object objType = System.Reflection.Assembly.Load(nameSpace).CreateInstance(fullclassName); //反射创建
            return (T) objType;
        }

        #region 从字符串生成Cookie

        /// <summary>
        /// 从字符串生成Cookie
        /// </summary>
        /// <param name="cookieString"></param>
        /// <returns></returns>
        public static HttpCookie GetCookieFromString(string cookieString)
        {
            //test=TestDateTime=2008-11-26 23:34:19&A=qqqqq&B=wwwww; domain=localhost; expires=Thu, 27-Nov-2008 15:34:19 GMT; path=/
            string cookieName = cookieString.Split('=')[0];
            string cookiePropertys = cookieString.Substring(cookieString.IndexOf('=') + 1);
            string[] propertys = Regex.Split(cookiePropertys, "; ");
            HttpCookie hc = new HttpCookie(cookieName);

            foreach (string p in propertys)
            {
                string[] property = p.Split('=');
                if (property.Length == 1)
                {
                    if (property[0] == "httponly")
                        hc.HttpOnly = true;
                }
                else if (property.Length > 2) //处理Cookie 的值
                {
                    foreach (string propertyValue in propertys[0].Split('&'))
                    {
                        string[] values = propertyValue.Split('=');
                        hc.Values[values[0]] = values[1];
                    }
                }
                else
                {
                    if (property[0] == "domain")
                    {
                        hc.Domain = property[1];
                    }
                    else if (property[0] == "expires")
                    {
                        //hc.Expires = Convert.ToDateTime(Property[1]);
                    }
                    else if (property[0] == "path")
                    {
                        hc.Path = property[1];
                    }
                    else if (property[0] == "secure")
                    {
                        hc.Secure = bool.Parse(property[1]);
                    }
                    else //处理Cookie 的值
                    {
                        string[] values = property[1].Split('&');
                        if (values.Length > 1)
                        {
                            hc.Values[values[0]] = values[1];
                        }
                        else
                        {
                            hc.Values[property[0]] = property[1];
                        }
                    }
                }
            }
            return hc;
        }

        #endregion

        /// <summary>
        /// 序列化为XML字符串
        /// </summary>
        /// <returns></returns>
        public static string Serialize(MemberInfo model)
        {
            return SerializableHelper.SerializeToString(model, DataFormart.Xml);
        }
    }
}